package com.sky.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Order;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.repository.query.Param;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper extends BaseMapper<Order> {
    /**
     * 根据订单号查询订单
     *
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Order getByNumber(String orderNumber);

    /**
     * 修改订单信息
     *
     * @param order
     */
    void update(Order order);


    /**
     * 订单分页查询
     *
     * @param ordersPage
     * @param dto
     * @return
     */
    Page<OrderVO> pageOrder(Page<OrderVO> ordersPage, @Param("dto") OrdersPageQueryDTO dto);


    /**
     * 根据日期统计营业额数据
     *
     * @param map
     * @return
     */
    BigDecimal sumAccountByMap(Map<String, Object> map);

    /**
     * 根据时间统计新增用户数量
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select("select count(*) from user where create_time > #{beginTime} and create_time < #{endTime}")
    Long sumNewUserByTime(LocalDateTime beginTime, LocalDateTime endTime);

    /**
     * 根据时间统计总用户数量
     *
     * @param endTime
     * @return
     */
    @Select("select count(*) from user where create_time < #{endTime}")
    Long sumTotalUserByTime(LocalDateTime endTime);

    /**
     * 根据时间统计每日有效订单数量
     *
     * @param map
     *
     * @return
     */

    Long countOrder(Map<String, Object> map);

    /**
     * 根据状态和下单时间查询订单
     *
     * @param pendingPayment
     * @param orderTime
     * @return
     */
    @Select("select * from orders where status = #{pendingPayment} and order_time < #{orderTime}")
    List<Order> selectByStatusAndOrderTimeLT(Integer pendingPayment, LocalDateTime orderTime);
}
